package com.bishe.demo.modules.tourism.service.impl;

import com.bishe.demo.modules.common.entity.City;
import com.bishe.demo.modules.common.entity.ResultEntity;
import com.bishe.demo.modules.tourism.dao.*;
import com.bishe.demo.modules.tourism.entity.*;
import com.bishe.demo.modules.tourism.entity.Process;
import com.bishe.demo.modules.tourism.service.RouteService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @Description RouteServiceImpl
 * @Author Lubb
 * @Date 2021/3/29 15:00
 */
@Service
@Transactional
public class RouteServiceImpl implements RouteService {
    @Autowired
    private RouteDao routeDao;
    @Autowired
    private RouteCityDao routeCityDao;
    @Autowired
    private TourismImageDao tourismImageDao;
    @Autowired
    private RouteImageDao routeImageDao;
    @Autowired
    private ProcessDao processDao;
    @Autowired
    private RouteEvaluationDao routeEvaluationDao;

    @Override
    @Transactional
    public ResultEntity<Route> addRoute(Route route) {
        routeDao.addRoute(route);
        Integer routeId = route.getId();
        List<City> viaPoints = route.getViaPoint();
        if (viaPoints.size() > 0) {
            for (City viaPoint : viaPoints) {
                RouteCity routeCity = new RouteCity();
                routeCity.setRouteId(routeId);
                routeCity.setCityId(viaPoint.getId());
                routeCityDao.addRouteCity(routeCity);
            }
        }
        List<TourismImage> tourismImages = route.getTourismImage();
        if (tourismImages.size()>0){
            for (TourismImage tourismImage : tourismImages) {
                tourismImageDao.addTourismImage(tourismImage);
                RouteImage routeImage = new RouteImage();
                routeImage.setRouteId(routeId);
                routeImage.setImageId(tourismImage.getId());
                routeImageDao.addRouteImage(routeImage);
            }
        }
        List<Process> processes = route.getProcesses();
        if (processes.size() > 0){
            for (Process process : processes) {
                process.setFk_route_id(routeId);
                processDao.addProcess(process);
            }
        }

        route = getRouteById(route.getId());
        return new ResultEntity<Route>(ResultEntity.ResultStatus.SUCCESS.code,
                "新增路线成功", route);
    }

    @Override
    @Transactional
    public ResultEntity<Route> deleteRouteById(Integer id) {
        routeDao.deleteRouteById(id);
        routeCityDao.deleteRouteCityByRouteId(id);
        routeImageDao.deleteRouteImageByRouteId(id);
        tourismImageDao.deleteTourismImageByRouteId(id);
        processDao.deleteProcessByRouteId(id);
        routeEvaluationDao.deleteRouteEvaluationByRouteId(id);

        return new ResultEntity<Route>(ResultEntity.ResultStatus.SUCCESS.code,
                "删除路线成功");
    }

    @Override
    @Transactional
    public ResultEntity<Route> updateRoute(Route route) {
        routeDao.updateRoute(route);
        Integer routeId = route.getId();
        routeCityDao.deleteRouteCityByRouteId(routeId);
        List<City> viaPoints = route.getViaPoint();
        if (viaPoints.size() > 0) {
            for (City viaPoint : viaPoints) {
                RouteCity routeCity = new RouteCity();
                routeCity.setRouteId(routeId);
                routeCity.setCityId(viaPoint.getId());
                routeCityDao.addRouteCity(routeCity);
            }
        }
        routeImageDao.deleteRouteImageByRouteId(routeId);
        tourismImageDao.deleteTourismImageByRouteId(routeId);
        List<TourismImage> tourismImages = route.getTourismImage();
        if (tourismImages.size()>0){
            for (TourismImage tourismImage : tourismImages) {
                tourismImageDao.addTourismImage(tourismImage);
                RouteImage routeImage = new RouteImage();
                routeImage.setRouteId(routeId);
                routeImage.setImageId(tourismImage.getId());
                routeImageDao.addRouteImage(routeImage);
            }
        }
        processDao.deleteProcessByRouteId(routeId);
        List<Process> processes = route.getProcesses();
        if (processes.size() > 0){
            for (Process process : processes) {
                process.setFk_route_id(routeId);
                processDao.addProcess(process);
            }
        }
        route = getRouteById(route.getId());
        return new ResultEntity<Route>(ResultEntity.ResultStatus.SUCCESS.code,
                "更新路线成功", route);
    }

    @Override
    public Route getRouteById(Integer id) {
        return routeDao.getRouteById(id);
    }

    @Override
    public List<Route> getRoutes() {
        return routeDao.getRoutes();
    }

    @Override
    public PageInfo<Route> getRoutesBySearchBean(RouteSearchBean searchBean) {
        searchBean.initSearchBean();
        PageHelper.startPage(searchBean.getCurrentPage(), searchBean.getPageSize());
        return new PageInfo<Route>(Optional.ofNullable(routeDao.getRoutesBySearchBean(searchBean))
                .orElse(Collections.emptyList()));
    }
}
